package com.newbingo.dao;

import java.util.ArrayList;
import java.util.List;

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.hibernate.Criteria;
import org.hibernate.LockMode;
import org.hibernate.Query;
import org.hibernate.criterion.Example;
import org.hibernate.criterion.Expression;
import org.hibernate.criterion.Projections;
import org.springframework.orm.hibernate3.support.HibernateDaoSupport;

import com.newbingo.dao.imple.IBettingCellDao;
import com.newbingo.entity.BettingCell;
import com.newbingo.util.Pager;

public class BettingCellDao extends HibernateDaoSupport implements IBettingCellDao {
	private static final Log log = LogFactory.getLog(BettingCellDao.class);

	/**
	 * 
	 * 添加新投注格方法
	 */
	public void save(BettingCell bettingCell) {
		log.debug("saving BettingCell instance");
		try {
			getSession().save(bettingCell);
			log.debug("save successful");
		} catch (RuntimeException re) {
			log.error("save failed", re);
			throw re;
		}
	}

	/**
	 * 删除投注格
	 */
	public void delete(BettingCell bettingCell) {
		log.debug("deleting bettingCell instance");
		try {
			BettingCell bc=this.findById(bettingCell.getId());
			getSession().delete(bettingCell);
			log.debug("delete successful");
		} catch (RuntimeException re) {
			log.error("delete failed", re);
			throw re;
		}
	}
	


	/** 
	 * 根据主键查询
	 * 
	 */
	public BettingCell findById(java.lang.Integer id) {
		log.debug("getting Game instance with id: " + id);
		try {
			BettingCell instance = (BettingCell) getSession().get("com.newbingo.entity.BettingCell",
					id);
			return instance;
		} catch (RuntimeException re) {
			log.error("get failed", re);
			throw re;
		}
	}

	/**
	 * 
	 * 根据实例查询
	 */
	public List findByExample(BettingCell instance) {
		log.debug("finding BettingCell instance by example");
		try {
			List results = getSession().createCriteria(
					"com.newbingo.entity.BettingCell").add(Example.create(instance))
					.list();
			log.debug("find by example successful, result size: "
					+ results.size());
			return results;
		} catch (RuntimeException re) {
			log.error("find by example failed", re);
			throw re;
		}
	}

	/**
	 * 查询属性
	 * 
	 */
	public List findByProperty(String propertyName, Object value) {
		log.debug("finding BettingCell instance with property: " + propertyName
				+ ", value: " + value);
		try {
			String queryString = "from BettingCell as model where model."
					+ propertyName + "= ?";
			Query queryObject = getSession().createQuery(queryString);
			queryObject.setParameter(0, value);
			return queryObject.list();
		} catch (RuntimeException re) {
			log.error("find by property name failed", re);
			throw re;
		}
	}

	/**
	 * 根据投注格名称查询
	 */
	public List findByName(Object name) {
		return findByProperty("name", name);
	}

	/**
	 *
	 */
	public List findAll() {
		log.debug("finding all BettingCell instances");
		try {
			String queryString = "from BettingCell";
			Query queryObject = getSession().createQuery(queryString);
			return queryObject.list();
		} catch (RuntimeException re) {
			log.error("find all failed", re);
			throw re;
		}
	}

	/**
	 *修改投注格信息
	 */
	public BettingCell merge(BettingCell detachedInstance) {
		log.debug("merging BettingCell instance");
		try {
			getSession().saveOrUpdate(detachedInstance);
			log.debug("merge successful");
			return null;
		} catch (RuntimeException re) {
			log.error("merge failed", re);
			throw re;
		}
	}



	/**
	 * ָ分页查询方法
	 * 
	 * @param start
	 * @param limit
	 * @param name
	 * @return
	 */
	public Pager findByPage(int start, int limit, Object name) {
		Pager pager = new Pager();
		Criteria cri = this.getSession().createCriteria(BettingCell.class);
		// 判断是否有查询类型条件
		if (name != null) {
			cri.createAlias("gamePlay", "g");
			cri.add(Expression.like("g.name", name));
		}
		cri.setProjection(Projections.rowCount());
		pager.setTotalSize(((Number) cri.uniqueResult()).intValue());
		cri.setProjection(null);
		cri.setResultTransformer(Criteria.ROOT_ENTITY);
		cri.setFirstResult(start);
		cri.setMaxResults(limit);
		List gameList = cri.list();
		pager.setData(gameList);
		return pager;
	}
	
}
